import React from "react";
import Relay from "react-relay";
import { message } from 'antd';
import {Button} from 'fly';
import AcceptOrderMutation from "../../../mutation/AcceptOrderMutation";//接单按钮点击调用
/*导出组件*/
export default class AcceptButton extends React.Component{
    constructor(props){
        super(props);
        this.state={
          order: [],
          isaccept: false
        }
        this.resaveorder = this.resaveorder.bind(this);
        this.enterpriseId = sessionStorage.getItem('enterpriseId')
    }


    componentWillMount(){
        this.setState({
          order: this.props.order
        },()=>{
          const { order } = this.state;
          let orderOperates = order.orderOperates ? order.orderOperates : [];
          if(order.status == 2 || order.status == 1) {
              orderOperates.map((item,index)=>{
                  if(item.status == 2 && item.toId == this.enterpriseId){   //状态等于2,当前公司已接收订单
                      this.setState({
                          isaccept: true,
                      });
                  } else {
                    this.setState({
                        isaccept: false,
                    });
                  }
              });
          }
          if(order.status>="3"){
              this.setState({
                  isaccept:true,
              });
          }
        })
    }
    // 接单按钮点击
    resaveorder(e){
        message.config({
          top: 30,
          duration: 3
        });

        Relay.Store.commitUpdate(
            new AcceptOrderMutation({
                orderId:this.state.order.oid,
                enId:this.enterpriseId,
                enquiryId:this.props.order.enquiryId
            }),
            {
                onFailure: () => {
                  message.error('接单失败')
                },
                onSuccess: (response) => {
                    let errorCode = response.acceptOrder.acceptOrder.errorCode;
                    if(errorCode == 0){
                        this.setState({
                            isaccept:true,
                        },()=>{
                          message.success('您已成功接单，请等待甲方签订合同。')
                          this.props.Orderdetail()
                        });
                    }
                }
            }
        )
    }

    render(){
        return (
            <div style={{margin:'0 auto 20px', textAlign:'center'}}>
                <Button className={this.state.isaccept?"order_detail_accept_btn_disn":""} onClick={this.resaveorder}>接单</Button>
                <br/>
            </div>
            // <Button className={this.state.isaccept?"order_detail_accept_btn_disn":"order_detail_accept_btn"}>接单</Button>
        )
    }
}
